package com.csxy.qiu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.qiu.entity.EduTeacher;
import com.csxy.qiu.entity.vo.TeacherQuery;
import com.csxy.qiu.mapper.EduTeacherMapper;
import com.csxy.qiu.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherMapper eduTeacherMapper;
    @Override
    public Page<EduTeacher> pageListTeacher(long page, long limit) {
        Page<EduTeacher> mypage = new Page<>(page,limit);
        Page<EduTeacher> eduTeacherPage = eduTeacherMapper.selectPage(mypage, null);
        return eduTeacherPage;
    }

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //创建条件
        if (teacherQuery == null){
            eduTeacherMapper.selectPage(pageParam, null);
        }
        //讲师名称
        if (teacherQuery.getName()!= null &&!"".equals(teacherQuery.getName())){
            queryWrapper.like("name",teacherQuery.getName());
        }
        //讲师级别
        if (teacherQuery.getLevel()!= null &&!"".equals(teacherQuery.getLevel())){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }
        //获取开始时间
        if (teacherQuery.getBegin()!= null &&!"".equals(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        //获取结束的时间
        if (teacherQuery.getEnd()!= null &&!"".equals(teacherQuery.getEnd())){
            queryWrapper.le("gmt_modified",teacherQuery.getEnd());
        }
        eduTeacherMapper.selectPage(pageParam, queryWrapper);

    }

    //查询前4个热门讲师
    @Override
//    @Cacheable(key = "'selectHotTeacher'",value = "teacher")
    public List<EduTeacher> selectHotTeacher() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 4");
        return baseMapper.selectList(wrapper);
    }

    //前端系统分页查询讲师的方法
    //前端名师
    @Override
    public Map<String, Object> getTeacherFrontPageList(Page<EduTeacher> teacherPage) {
        // 前台系统分页查询讲师的方法
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        // 把分页数据封装到pageTeacher对象中
        baseMapper.selectPage(teacherPage, wrapper);
        // 把分页的数据获取出来返回一个map集合
        HashMap<String, Object> map = new HashMap<>();
        // 总记录数
        long total = teacherPage.getTotal();
        // 当前页
        long current = teacherPage.getCurrent();
        // 每页记录数
        long size = teacherPage.getSize();
        // 查询到的对象
        List<EduTeacher> teacherList = teacherPage.getRecords();
        // 总页数
        long pages = teacherPage.getPages();
        // 是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();
        // 是否有下一页
        boolean hasNext = teacherPage.hasNext();
        // 将数据封装到map中返回
        map.put("total",total);
        map.put("current", current);
        map.put("size", size);
        map.put("list", teacherList);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);
        map.put("pages", pages);

        return map;
    }
}
